<template>
    <a-modal :title="$t('添加用户(组)')" v-model='isShow' @cancel='handleClose' width='680px' @ok='handleCreate'
        :cancelText="$t('Cancel')" :okText="$t('Sure')" :maskClosable='false' v-dragModal>
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-spin :spinning="loading">
                <a-form-model-item :label="$t('共享类型')" prop="user">
                    <a-radio-group v-model="form.share_type" @change="shareChange">
                        <a-radio v-for="item of shareGroup" :value="item.value" :key="item.value">{{ item.label
                            }}</a-radio>
                        <!-- <template v-if="$i18n.locale == 'zh'">
                        <a-radio v-for="item of shareGroupZh" :value="item.value" :key="item.value">{{ item.label }}</a-radio>
                    </template>
<template v-else>
                        <a-radio v-for="item of shareGroupEn" :value="item.value" :key="item.value">{{ item.label }}</a-radio>
                    </template> -->
                    </a-radio-group>
                </a-form-model-item>
                <a-form-model-item :label="$t('用户类型')" prop="pwd">
                    <a-radio-group v-model="form.user_type" @change="userChange">
                        <a-radio v-for="item of userGroup" :value="item.value" :key="item.value">{{ item.label
                            }}</a-radio>
                    </a-radio-group>
                    <a-transfer :data-source="transferList" show-search :filter-option="filterOption"
                        :target-keys="targetKeys" :render="item => item.title" @change="handleChange"
                        @search="handleSearch" />
                </a-form-model-item>
                <a-form-model-item :label="$t('权限')" prop="confirm_password">
                    <a-radio-group v-model="form.permission">
                        <a-radio v-for="item of authorityGroup" :value="item.value" :key="item.value">{{ item.label
                            }}</a-radio>
                    </a-radio-group>
                </a-form-model-item>
            </a-spin>
        </a-form-model>
    </a-modal>
</template>

<script>
export default {
    data() {
        return {
            isShow: false,
            loading: false,
            labelCol: { span: 6 },
            wrapperCol: { span: 18 },
            form: {
                share_type: 1,
                user_type: 1,
                permission: 1,
            },
            rules: {
            },
            shareGroup: [
                { value: 1, label: '本地' },
                { value: 2, label: 'AD' },
            ],
            userGroup: [],
            localGroup: [
                { value: 1, label: '本地用户' },
            ],
            ADGroup: [
                { value: 1, label: 'AD用户' },
                { value: 2, label: 'AD用户组' },
            ],
            authorityGroup: [
                { value: 1, label: '只读' },
                { value: 2, label: '读写' },
            ],
            targetKeys: [],
            transferList: [],
            sambaMap: new Map([]),
            adMap: new Map([]),
            adGroupMap: new Map([]),
            sambaUserList: [],
            ADUserList: [],
            ADGroupList: [],
            targetCopy: [],
        };
    },
    props: {
        defaultList: {
            type: Array,
            default: []
        },
    },
    async mounted() {
        await this.getSambaList()
        await this.getADUserList()
        await this.getADGroupList()
        this.userGroup = this.localGroup
        this.transferList = this.sambaUserList
        // this.targetCopy = this.targetKeys
    },
    watch: {
        defaultList(value) {
            if (this.form.share_type == 1 && this.form.user_type == 1) this.transferList = this.matchList(this.sambaUserList, value)
            else if (this.form.share_type == 2 && this.form.user_type == 1) this.transferList = this.matchList(this.ADUserList, value)
            else if (this.form.share_type == 2 && this.form.user_type == 2) this.transferList = this.matchList(this.getADGroupList, value)
        }
    },
    methods: {
        matchList(list1, list2) {
            return list1.filter(item => !list2.find(subItem => subItem.index == item.key))
        },
        async getSambaList() {
            this.loading = true
            const { data } = await this.$api.samba.getSambaApi()
            const map = new Map([])
            this.sambaUserList = data.samba_user.map((item, index) => {
                map.set('samba' + item.id, item.username)
                const obj = { key: 'samba' + item.id, title: item.username, chosen: false }
                return obj
            })
            this.sambaMap = map
            this.loading = false
        },
        async getADUserList() {
            this.loading = true
            const { data } = await this.$api.authentication.getUser({ id: '1' })
            const map = new Map([])
            this.ADUserList = data.user.map((item, index) => {
                map.set('ad' + item.user_id, item.user_name)
                const obj = { key: 'ad' + item.user_id, title: item.user_name, chosen: false }
                return obj
            })
            this.adMap = map
            this.loading = false
        },
        async getADGroupList() {
            this.loading = true
            const { data } = await this.$api.authentication.getUserGroup({ id: '1' })
            const map = new Map([])
            this.ADGroupList = data.user.map((item, index) => {
                map.set('adGroup' + item.id, item.department)
                const obj = { key: 'adGroup' + item.id, title: item.department, chosen: false }
                return obj
            })
            this.adGroupMap = map
            this.loading = false
        },
        shareChange(e) {
            const checked = e.target.value
            if (checked == 1) {
                this.userGroup = this.localGroup
                this.transferList = this.sambaUserList
            }
            else if (checked == 2) {
                this.userGroup = this.ADGroup
                this.transferList = this.ADUserList
            }
        },
        userChange(e) {
            const value = e.target.value
            if (value === 2) this.transferList = this.ADGroupList
            else if (value == 1) this.transferList = this.ADUserList
        },
        handleCreate() {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    const { share_type, user_type, permission } = this.form
                    const list = []
                    if (share_type == 1 && user_type == 1) this.targetKeys.map((item, index) => {
                        if (this.sambaMap.get(item)) {
                            const obj = { user: this.sambaMap.get(item), share_type, user_type, permission, index: item }
                            list.push(obj)
                        }
                    })
                    else if (share_type == 2 && user_type == 1) this.targetKeys.map((item, index) => {
                        if (this.adMap.get(item)) {
                            const obj = { user: this.adMap.get(item), share_type, user_type, permission, index: item }
                            list.push(obj)
                        }
                    })
                    else if (share_type == 2 && user_type == 2) this.targetKeys.map((item, index) => {
                        if (this.adGroupMap.get(item)) {
                            const obj = { user: this.adGroupMap.get(item), share_type, user_type, permission, index: item }
                            list.push(obj)
                        }
                    })
                    this.$emit('submit-form', list)
                    this.isShow = false
                } else {
                    return false;
                }
            });
        },
        handleClose() {
            this.$refs['ruleForm'].resetFields();
            // this.targetKeys = this.targetCopy
            this.targetKeys = []
            this.isShow = false
        },
        confirmValidator(rule, value, callback) {
            if (value) {
                if (value !== this.form.pwd) callback('两次密码请输入一致')
                else callback()
            }
        },
        filterOption(inputValue, option) {
            return option.description.indexOf(inputValue) > -1;
        },
        handleChange(targetKeys, direction, moveKeys) {
            let list = []
            if (direction == 'right') {
                if (this.form.share_type == 1 && this.form.user_type == 1) list = moveKeys.map(item => {
                    if (item.includes('samba')) return item
                })
                else if (this.form.share_type == 2 && this.form.user_type == 1) list = moveKeys.map(item => {
                    if (item.includes('ad')) return item
                })
                else if (this.form.share_type == 2 && this.form.user_type == 2) list = moveKeys.map(item => {
                    if (item.includes('adGroup')) return item
                })
                this.targetKeys = [...this.targetKeys, ...list]
            } else {
                this.targetKeys = targetKeys
            }
            this.targetCopy = this.targetKeys
        },
        handleSearch(dir, value) {
            console.log('search:', dir, value);
        },
    }
}
</script>

<style lang="scss" scoped>
::v-deep .ant-transfer-list {
    height: 260px;
    width: 225px;
}

::v-deep .ant-modal-body {
    padding: 24px 0;
}
</style>
